<?php
/**
 * 用户管理控制器
 * @authors 大个子 (354449881@qq.com)
 * @date    2018-01-12 09:09:09
 * @version 1.0
 */
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController {
	private $db;

	public function _initialize(){
		parent::_initialize();
		$this->db = D('User');
	}
	public function index() {
		// parent::userauth2(2);
		$where=array();
		if (I('get.keyword','','htmlspecialchars')) {
			$where['username'] = I('get.keyword','','htmlspecialchars');
		}
		$data = D('AuthGroupAccess')->getAllData($where);

		$this->assign('volist',$data['volist']);
		$this->assign('page',$data['show']);					//输出分页
		$this->assign('keyword',$where['username']);
		$this->co = $data['count'];//原系统中不知道做什么用的
		$this->display();
	}
	/**
     * 添加管理员
     */
    public function useradd(){
        if(IS_AJAX){
            $data=I('post.');
            $data['password'] = sha1(md5($data['password']));
            $data['dtime'] = time();
            $result=$this->db->addData($data);
            if($result!==false){
                if (!empty($data['group_ids'])) {
                	$data['group_ids']=substr($data['group_ids'], 0,-1);
            		$group_ids=explode(',',$data['group_ids']);
                    foreach ($group_ids as $k => $v) {
                        $group=array(
                            'uid'=>$result,
                            'group_id'=>$v
                            );
                        D('AuthGroupAccess')->addData($group);
                    }                   
                }
                // 操作成功
                parent::operating(__ACTION__,0,'新增用户：'.$data['username']);
				R('Public/errjson',array('ok'));
            }else{
                // 操作失败
            	parent::operating(__ACTION__,1,'新增失败：'.$data['username']);
				R('Public/errjson',array($this->db->getError()));
            }
        }else{
            $data=D('AuthGroup')->select();
            $department=D('Department')->select();
            $assign=array(
                'data'=>$data,
                'department'=>$department
                );
            $this->assign($assign);
            $this->display();
        }
    }

    /**
     * 修改管理员
     */
    public function useredit(){
        if(IS_POST){

            $data=I('post.');
				
            // 组合where数组条件
            $uid=$data['id'];
            $map=array(
                'id'=>$uid
                );
            
            // 如果修改密码则md5
            if (!empty($data['password'])) {
                $data['password']=sha1(md5($data['password']));
            }else{
                unset($data['password']);
            }
            $result=$this->db->editData($map,$data);
            if($result!==false){
                // 修改权限
	            D('AuthGroupAccess')->deleteData(array('uid'=>$uid));
	            if (!empty($data['group_ids'])) {
	            	$data['group_ids']=substr($data['group_ids'], 0,-1);
	        		$group_ids=explode(',',$data['group_ids']);
	                foreach ($group_ids as $k => $v) {
	                    $group=array(
	                        'uid'=>$uid,
	                        'group_id'=>$v
	                        );
	                    D('AuthGroupAccess')->addData($group);
	                }                   
	            }
	            parent::operating(__ACTION__,0,'更改用户资料：'.$data['username']);
				R('Public/errjson',array('ok'));
            }else{
            	parent::operating(__ACTION__,1,'更新失败：'.$data['username']);
				R('Public/errjson',array($user->getError()));
            }
        }else{
            $id=I('get.id',0,'intval');
            // 获取用户数据
            $user_data=M('User')->find($id);

            // 获取已加入用户组
            $group_data=M('AuthGroupAccess')
                ->where(array('uid'=>$id))
                ->getField('group_id',true);
            // 全部用户组
            $data=D('AuthGroup')->select();

            // 获取所有部门
            $department=D('Department')->select();
            $assign=array(
                'data'=>$data,
                'user_data'=>$user_data,
                'group_data'=>$group_data,
                'department'=>$department
                );
            $this->assign($assign);
            $this->display();
        }
    }
    //修改密码
    public function editpsw(){
            if(I('post.')){
                $post=I('post.');
                if(!$post['old_pwd']||!$post['new_pwd']||!$post['re_pwd']){
                    $arr['info']='新旧密码不可以为空';
                    $arr['status']=0;
                    $this->ajaxReturn($arr);
                }
               
                if($post['new_pwd']==$post['re_pwd']){
                    if($post['old_pwd']==$post['new_pwd']){
                        $arr['info']='新旧密码相同';
                        $arr['status']=0;
                    }else{
                        
                        $password=M('User')->where('id='.$_SESSION['user']['id'])->getField('password');
                        if($password==md5($post['old_pwd'])){
                            $save['password']=md5($post['new_pwd']);
                            $res=M('User')->where('id='.$_SESSION['user']['id'])->data($save)->save();
                            if($res){
                                unset($_SESSION['user']);
                                $arr['status']=1;
                                $arr['info']='密码更新成功';
                            }else{
                                $arr['info']='密码更新失败';
                                $arr['status']=0;
                            }
                        }else{
                            $arr['info']='旧密码输入不正确';
                            $arr['status']=0;
                        
                        }
                    }
                }else{
                    $arr['info']='新密码两次输入不一致';
                    $arr['status']=0;
                   
                }
                 $this->ajaxReturn($arr);
            }else{
                $this->display();
            }
            
    }
    //设置个性头像
    public function editphoto(){

        if(!empty($_FILES['user-avatar']['name'])){
            $res=$this->uploadfile($_FILES['user-avatar']);
            $data['face']=$res;
            $re=D('User')->where('id='.$_SESSION['user']['id'])->data($data)->save();
            if($re){
                unlink('.'.$_SESSION['user']['avatar']);
                $_SESSION['user']['avatar']=$res;
                $arr['status']=1;
                $arr['info']='头像更新成功';
            }else{
                $arr['info']='头像更新失败';
                $arr['status']=0;
            }
            $this->ajaxReturn($arr);
        }else{
            $this->display(); 
        }
        
    }
	//*******************用户组**********************
    /**
     * 用户组列表
     */
    public function group(){
        $data=D('AuthGroup')->select();
        $assign=array(
            'data'=>$data
            );
        $this->assign($assign);
        $this->display();
    }

    /**
     * 添加用户组
     */
    public function add_group(){
        $data=I('post.');
        unset($data['id']);
        $result=D('AuthGroup')->addData($data);
        if ($result) {
            $this->success('添加成功',U('Admin/Rule/group'));
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * 修改用户组
     */
    public function edit_group(){
        $data=I('post.');
        $map=array(
            'id'=>$data['id']
        );
        $result=D('AuthGroup')->editData($map,$data);
        if ($result!==false) {
            $this->success('修改成功',U('Admin/Rule/group'));
        }else{
            $this->error('修改失败');
        }
    }

    /**
     * 删除用户组
     */
    public function delete_group(){
        $id=I('get.id');
        $map=array(
            'id'=>$id
            );
        $result=D('AuthGroup')->deleteData($map);
        if ($result) {
            $this->success('删除成功',U('Admin/Rule/group'));
        }else{
            $this->error('删除失败');
        }
    }

//*****************权限-用户组*****************
    /**
     * 分配权限
     */
    public function rule_group(){
        if(IS_POST){
            $data=I('post.');
            $map=array(
                'id'=>$data['id']
                );
            $data['rules']=implode(',', $data['rule_ids']);
            $result=D('AuthGroup')->editData($map,$data);
            if ($result) {
                $this->success('操作成功',U('Admin/Rule/group'));
            }else{
                $this->error('操作失败');
            }
        }else{
            $id=I('get.id');
            // 获取用户组数据
            $group_data=M('Auth_group')->where(array('id'=>$id))->find();
            $group_data['rules']=explode(',', $group_data['rules']);
            // 获取规则数据
            $rule_data=D('AuthRule')->getTreeData('level','id','title');
            $assign=array(
                'group_data'=>$group_data,
                'rule_data'=>$rule_data
                );
            $this->assign($assign);
            $this->display();
        }

    }
//******************用户-用户组*******************
    /**
     * 添加成员
     */
    public function check_user(){
        $username=I('get.username','');
        $group_id=I('get.group_id');
        $group_name=M('Auth_group')->getFieldById($group_id,'title');
        $uids=D('AuthGroupAccess')->getUidsByGroupId($group_id);
        // 判断用户名是否为空
        if(empty($username)){
            $user_data='';
        }else{
            $user_data=M('User')->where(array('username'=>$username))->select();
        }
        $assign=array(
            'group_name'=>$group_name,
            'uids'=>$uids,
            'user_data'=>$user_data,
            );
        $this->assign($assign);
        $this->display();
    }

    /**
     * 添加用户到用户组
     */
    public function add_user_to_group(){
        $data=I('get.');
        $map=array(
            'uid'=>$data['uid'],
            'group_id'=>$data['group_id']
            );
        $count=M('AuthGroupAccess')->where($map)->count();
        if($count==0){
            D('AuthGroupAccess')->addData($data);
        }
        $this->success('操作成功',U('Admin/Rule/check_user',array('group_id'=>$data['group_id'],'username'=>$data['username'])));
    }

    /**
     * 将用户移除用户组
     */
    public function delete_user_from_group(){
        $map=I('get.');
        $result=D('AuthGroupAccess')->deleteData($map);
        if ($result) {
            $this->success('操作成功',U('Admin/Rule/admin_user_list'));
        }else{
            $this->error('操作失败');
        }
    }

	
}
?>